<template>
    <div class="register-container">
        <el-card class="register-card" shadow="always">
            <div class="register-header">
                <h2 class="register-title">用户注册</h2>
            </div>
            <el-form :model="registerForm" :rules="rules" ref="registerFormRef" label-width="0">
                <el-form-item prop="name">
                    <el-input v-model="registerForm.name" placeholder="请输入姓名">
                        <template #prefix>
                            <el-icon>
                                <User />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="username">
                    <el-input v-model="registerForm.username" placeholder="请输入账号">
                        <template #prefix>
                            <el-icon>
                                <UserFilled />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="registerForm.password" type="password" placeholder="请输入密码">
                        <template #prefix>
                            <el-icon>
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="confirmPassword">
                    <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请确认密码">
                        <template #prefix>
                            <el-icon>
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="phone">
                    <el-input v-model="registerForm.phone" placeholder="请输入手机号">
                        <template #prefix>
                            <el-icon>
                                <Phone />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitRegister" style="width: 48%;">注册</el-button>
                    <el-button @click="resetForm" style="width: 48%; margin-left: 4%;">重置</el-button>
                </el-form-item>
                <div class="login-link">
                    已有账号？<a href="#" @click.prevent="router.push('/login')" class="login-button">立即登录</a>
                </div>
            </el-form>
        </el-card>
    </div>
</template>

<script setup>
import { ref } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { User, UserFilled, Lock, Phone } from '@element-plus/icons-vue'

const registerForm = ref({
    name: "",
    username: "",
    password: "",
    confirmPassword: "",
    phone: "",
});

const rules = {
    name: [
        { required: true, message: "请输入姓名", trigger: "blur" },
    ],
    username: [
        { required: true, message: "请输入账号", trigger: "blur" },
    ],
    password: [
        { required: true, message: "请输入密码", trigger: "blur" },
        { min: 6, message: "密码长度不能少于 6 位", trigger: "blur" },
    ],
    confirmPassword: [
        { required: true, message: "请确认密码", trigger: "blur" },
        {
            validator: (rule, value, callback) => {
                if (value !== registerForm.value.password) {
                    callback(new Error("两次输入的密码不一致"));
                } else {
                    callback();
                }
            },
            trigger: "blur",
        },
    ],
    phone: [
        { required: true, message: "请输入手机号", trigger: "blur" },
        { pattern: /^\d{11}$/, message: "手机号格式不正确", trigger: "blur" },
    ],
};

const registerFormRef = ref(null);
const router = useRouter();

const submitRegister = () => {
    registerFormRef.value.validate(async (valid) => {
        if (valid) {
            try {
                const response = await axios.post(
                    "http://localhost:3000/api/users/useradd",
                    registerForm.value
                );
                ElMessage.success("注册成功");
                router.push("/login"); // 注册成功后跳转到登录页面
            } catch (error) {
                ElMessage.error("注册失败，请重试");
            }
        } else {
            console.log("错误输入!");
            return false;
        }
    });
};

const resetForm = () => {
    registerFormRef.value.resetFields();
};
</script>

<style scoped>
.register-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #f0f2f5, #cfd9df);
    padding: 20px;
}

.register-card {
    width: 380px;
    padding: 30px;
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.08);
    border-radius: 12px;
    background-color: #ffffff;
}

.register-header {
    text-align: center;
    margin-bottom: 35px;
}

.register-title {
    font-size: 28px;
    color: #333;
    margin: 0 0 8px 0;
    font-weight: 600;
}

.register-subtitle {
    color: #909399;
    font-size: 16px;
}

:deep(.el-form-item) {
    margin-bottom: 25px;
}

:deep(.el-input) {
    --el-input-height: 45px;
    font-size: 14px;
}

:deep(.el-input__wrapper) {
    box-shadow: 0 0 0 1px #dcdfe6 inset;
    padding: 0 15px;
    transition: all 0.2s;
}

:deep(.el-input__wrapper:hover) {
    box-shadow: 0 0 0 1px #409eff inset;
}

:deep(.el-input__prefix) {
    display: flex;
    align-items: center;
    margin-right: 8px;
}

:deep(.el-icon) {
    font-size: 18px;
    color: #909399;
}

:deep(.el-input__inner) {
    margin-left: 4px;
}

.el-button {
    width: 100%;
    height: 45px;
    font-size: 16px;
    font-weight: 500;
    border-radius: 4px;
    margin-top: 10px;
    transition: all 0.3s;
}

.el-button:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.login-link {
    text-align: center;
    margin-top: 20px;
    color: #606266;
    font-size: 14px;
}

.login-button {
    color: #409eff;
    text-decoration: none;
    margin-left: 4px;
    font-weight: bold;
    cursor: pointer;
}

.login-button:hover {
    color: #66b1ff;
    text-decoration: underline;
}
</style>